package com.bootdo.collection.utils;


import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicHeader;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.*;

public class UploadStationUtil {
    public static R upload(String path,String title,List<String> imageList,String strUrl) {
        HttpClient httpclient =  HttpClients.createDefault();
        ImageForm imageForm = new ImageForm();
        initImageForm(imageForm, path, title, imageList);
        try {
            URL url = new URL(strUrl);
            URI uri = new URI(url.getProtocol(), url.getHost(), url.getPath(), url.getQuery(), null);
            HttpPost post = new HttpPost(uri); //创建表单
            ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();//用于存放表单数据.
            Map<String, String> map = new HashMap<>();
            //判断是否有缩略图
//            if(thumbnail != null){
//                map.put("litpic",thumbnail);
//            }
            map.put("title", imageForm.getTitle());
            map.put("cat_id", imageForm.getCat_id());
            map.put("ftppathyear", imageForm.getFtppathyear());
            map.put("ftppathmonth", imageForm.getFtppathmonth());
            map.put("ftppathday",imageForm.getFtppathday());
            map.put("ftppath", imageForm.getFtppath());
            map.put("imglistpagenums", imageForm.getImglistpagenums());
            map.put("click", imageForm.getClick());
            map.put("send_time", imageForm.getSend_time());
            map.put("status", imageForm.getStatus());
            map.put("sorttitle",imageForm.getTitle());
            map.put("add_type", imageForm.getAdd_type());
            //遍历map 将其中的数据转化为表单数据
            for (Map.Entry<String, String> entry :
                    map.entrySet()) {
                pairs.add(new BasicNameValuePair(entry.getKey(), entry.getValue()));
            }
            for(String img:imageList){
                pairs.add(new BasicNameValuePair("imglist[]",img));
            }
            //对表单数据进行编码进行编码
          UrlEncodedFormEntity urlEncodedFormEntity = new UrlEncodedFormEntity(pairs,"utf-8");
          post.setEntity(urlEncodedFormEntity);
          post.setHeader(new BasicHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8"));
          String cookieValue = "yxosyaml_admlogin=1%7C" + StringUtils.replaceAll(UUID.randomUUID().toString(),"-","");
          post.addHeader("Cookie",cookieValue);
          HttpResponse response = httpclient.execute(post);//发送数据.提交表单
          String  body = EntityUtils.toString(response.getEntity());
          if(body.contains("添加文档成功")){
              return R.ok();
          }else {
              return R.error();
          }

//                HttpUriRequest login = RequestBuilder.post()
//                        .setUri(new URI(url))
//                        .addParameter("title", imageForm.getTitle())
//                        .addParameter("cat_id", imageForm.getCat_id())
//                        .addParameter("ftppathyear", imageForm.getFtppathyear())
//                        .addParameter("ftppathmonth", imageForm.getFtppathmonth())
//                        .addParameter("ftppath", imageForm.getFtppath())
//                        .addParameter("imglist", imageForm.getImageList().toString())
//                        .addParameter("imglistpagenums", imageForm.getImglistpagenums())
//                        .addParameter("click", imageForm.getClick())
//                        .addParameter("send_time", imageForm.getSend_time())
//                        .addParameter("status", imageForm.getStatus())
//                        .addParameter("add_type", imageForm.getAdd_type())
//                        .build();
//                CloseableHttpResponse response = (CloseableHttpResponse) httpclient.execute(login);
//            } catch (URISyntaxException e) {
//                e.printStackTrace();
//            } catch (ClientProtocolException e) {
//                e.printStackTrace();
//            } catch (IOException e) {
//                e.printStackTrace();
            }catch(Exception e){
                 e.printStackTrace();
            }
            return null;
        }
    private static void initImageForm(ImageForm imageForm,String path,String title,List<String> imageList){
        String[] pathArray = path.split("/");
        for(int index = 0;index < pathArray.length;index++){
            if(index == 1){
                imageForm.setFtppathyear(pathArray[index]);
            }
            if(index == 2){
                String mix = pathArray[index];
                imageForm.setFtppathmonth(mix.substring(0,2));
                imageForm.setFtppathday(mix.substring(2));
            }
            if(index == 3){
                imageForm.setFtppath(pathArray[index]);
            }
            imageForm.setImageList(imageList);
            imageForm.setTitle(title);

        }
    }

    private static class ImageForm{
       public ImageForm() {
            imglistpagenums = "1";
            cat_id = "1";
            status = "0";
            add_type = "Imagelist";
            Random random = new Random();
            click = String.valueOf(random.nextInt(100) + 500);
            Date date = new Date();
            send_time =  new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
        }
        private String title;
        //栏目id
        private String cat_id;
        //目录年
        private String ftppathyear;
        //目录月
        private String ftppathmonth;
        //目录日
        private String ftppathday;
        //目录id
        private String ftppath;
        //图集
        private List<String> imageList;
        private String imglistpagenums;
        //初始化点击数
        private String click;
        //当前时间
        private String send_time;
        //状态
        private String status;
        //类型
        private String add_type;

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }

        public String getCat_id() {
            return cat_id;
        }

        public void setCat_id(String cat_id) {
            this.cat_id = cat_id;
        }

        public String getFtppathyear() {
            return ftppathyear;
        }

        public void setFtppathyear(String ftppathyear) {
            this.ftppathyear = ftppathyear;
        }

        public String getFtppathmonth() {
            return ftppathmonth;
        }

        public void setFtppathmonth(String ftppathmonth) {
            this.ftppathmonth = ftppathmonth;
        }

        public String getFtppathday() {
            return ftppathday;
        }

        public void setFtppathday(String ftppathday) {
            this.ftppathday = ftppathday;
        }

        public String getFtppath() {
            return ftppath;
        }

        public void setFtppath(String ftppath) {
            this.ftppath = ftppath;
        }

        public List<String> getImageList() {
            return imageList;
        }

        public void setImageList(List<String> imageList) {
            this.imageList = imageList;
        }

        public String getImglistpagenums() {
            return imglistpagenums;
        }

        public void setImglistpagenums(String imglistpagenums) {
            this.imglistpagenums = imglistpagenums;
        }

        public String getClick() {
            return click;
        }

        public void setClick(String click) {
            this.click = click;
        }

        public String getSend_time() {
            return send_time;
        }

        public void setSend_time(String send_time) {
            this.send_time = send_time;
        }

        public String getStatus() {
            return status;
        }

        public void setStatus(String status) {
            this.status = status;
        }

        public String getAdd_type() {
            return add_type;
        }

        public void setAdd_type(String add_type) {
            this.add_type = add_type;
        }

    }
}
